package com.onluy.sysman.controller.sysmgr;

import com.alibaba.fastjson.JSON;
import com.onluy.frameworkcloud.Param.*;
import com.onluy.frameworkcloud.data.bean.PageData;
import com.onluy.frameworkcloud.data.util.InParam;
import com.onluy.frameworkcloud.base.AppWalkController;
import com.onluy.sysman.entity.sysmgr.TfMgrUser;
import com.onluy.sysman.param.UserParam;
import com.onluy.sysman.service.TfMgrUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Validated
@RestController
@RequestMapping("/user")
@Api(value="user", description="/用户管理接口", position=1 )
public class TfMgrUserController extends AppWalkController {

	//调用服务
	@Autowired
	private TfMgrUserService tfMgrUserService;

	@RequestMapping(value={"/target"},method={ RequestMethod.POST})
	@ApiOperation(value="目标", notes="根据userId获取信息")
	public RspInfo<TfMgrUser> dotarget(@RequestBody ReqInfo<ReqHead, UserParam> reqInfo   ) throws Exception {
		RspInfo<TfMgrUser> jsonResult = new RspInfo<TfMgrUser>();
		log.info("------------>Model reqInfo : " + JSON.toJSONString(reqInfo));

		if( null == reqInfo.getReqBody().getUserId() )throw new Exception("缺少必要属性userId！");
		TfMgrUser tfMgrUser = new TfMgrUser();
		tfMgrUser.setUserId(  reqInfo.getReqBody().getUserId()  ).asCondition();
		tfMgrUser = tfMgrUserService.model(tfMgrUser);
		jsonResult.setRspCode(0);
		jsonResult.setRspData(tfMgrUser);
		return jsonResult;
	}



	@RequestMapping(value={ "/add"},method={RequestMethod.POST })
	@ApiOperation(value="新增", notes="新增部门")
	public RspInfo<InParam> doAdd(@RequestBody @Valid ReqInfo<ReqHead,TfMgrUser> reqInfo )  throws Exception{
		RspInfo<InParam> jsonResult = new RspInfo<InParam>();
		int num = 0;
		TfMgrUser tfMgrUser = reqInfo.getReqBody();
		num = tfMgrUserService.add(tfMgrUser);
		if (num > 0) {
			jsonResult.setRspCode(0);
			jsonResult.setRspDesc("增加成功！");
		} else {
			jsonResult.setRspDesc("增加失败！");
		}
		return jsonResult;
	}

	@RequestMapping(value={ "/update"},method={RequestMethod.POST})
	@ApiOperation(value="更新", notes="根据userId更新用户")
	public RspInfo<InParam> doUpdate(      @RequestBody @Valid  ReqInfo<ReqHead,TfMgrUser> reqInfo ) throws Exception {
		RspInfo<InParam> jsonResult=new RspInfo<InParam>();
		int num=0;

		if( null == reqInfo.getReqBody().getUserId() )throw new Exception("缺少必要属性userId！");

		TfMgrUser tfMgrUser = reqInfo.getReqBody();
		tfMgrUser.setUserId(  reqInfo.getReqBody().getUserId()  ).asCondition();
		num=tfMgrUserService.update( tfMgrUser);
		if(  num>0 ){
			jsonResult.setRspCode(0);
			jsonResult.setRspDesc("修改成功！");
		}
		else {
			jsonResult.setRspDesc("修改失败！");
		}
		return jsonResult;
	}

 
		

	@RequestMapping(value={"/del"},method={RequestMethod.POST})
	@ApiOperation(value="删除", notes="根据userId删除用户")
	public RspInfo<InParam> doDel( @RequestBody    ReqInfo<ReqHead,UserParam> reqInfo) throws Exception {
		RspInfo<InParam> jsonResult=new RspInfo<InParam>();
		TfMgrUser tfMgrUser = new TfMgrUser();
		int num=1;
		try {
			if( null == reqInfo.getReqBody().getUserId() )throw new Exception("缺少必要属性userId！");
			tfMgrUser.setUserId(  reqInfo.getReqBody().getUserId()  ).asCondition();

			num=tfMgrUserService.del( tfMgrUser);
		} catch (Exception e) {
			jsonResult.setRspDesc("删除异常:"+e.getMessage());
			e.printStackTrace();
		}
		if(  num>0 ){
			jsonResult.setRspCode(0);
			jsonResult.setRspDesc("删除成功！");
		}
		else {
			jsonResult.setRspDesc("删除失败！");
		}
		return jsonResult;
	}


	@ResponseBody
	@RequestMapping(value={ "/list"},method={RequestMethod.POST })
	@ApiOperation(value="列表", notes="部门列表明细")
	public RspInfo<PageData<TfMgrUser>>  doList(     @RequestBody    ReqInfo<ParamPagination, TfMgrUser> reqInfo )throws Exception {
		RspInfo<PageData<TfMgrUser>> jsonResult=new RspInfo<PageData<TfMgrUser>>();

		this.MakeLikeEntity(reqInfo.getReqBody());

		PageData<TfMgrUser> list =tfMgrUserService.list(reqInfo.getReqBody(),FormatPagination(reqInfo.getReqHead()));
		jsonResult.setRspCode(0);
		jsonResult.setRspData(list);
		return jsonResult;
	}


	public  void  MakeLikeEntity(TfMgrUser tfMgrUser){

		if(tfMgrUser == null )return;

		if(null!=tfMgrUser.getUserCode()){
			tfMgrUser.setUserCode(tfMgrUser.getUserCode()).asCondition();
		}
		if(null!=tfMgrUser.getUserName()){
			tfMgrUser.setUserName(null).getCondition().andLike( "%"+tfMgrUser.getUserName()+"%" );
		}

	}


}
